package cn.skycity.framework.core.config;

import cn.skycity.framework.core.constants.GlobalConstants;
import cn.skycity.framework.core.constants.SecurityConstants;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Enumeration;
import java.util.List;
import java.util.stream.Collectors;

/**
 * openfeign 拦截器
 *
 * @Author: liguang5
 * @Date: 2019-06-12
 * @Version: 1.0
 */
@Configuration
@EnableConfigurationProperties(IgnoreProperties.class)
@Slf4j
public class FeignConfiguration implements RequestInterceptor {

    @Resource
    private IgnoreProperties ignoreProperties;

    @Override
    public void apply(RequestTemplate requestTemplate) {
        requestTemplate.header(GlobalConstants.FEIGN_FLAG, "Y");
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        // gateway 中调用时获取不到当前 session
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            Enumeration<String> headerNames = request.getHeaderNames();
            if (headerNames != null) {
                List<String> urls = ignoreProperties.getUrls();
                String[] ignores = urls.toArray(new String[0]);
                while (headerNames.hasMoreElements()) {
                    String name = headerNames.nextElement();
                    String values = request.getHeader(name);
                    log.info("header name: {}, value: {}", name, values);
//                    if (CommonConstants.AUTHORIZATION.equalsIgnoreCase(name)
//                            && !StrUtil.containsAnyIgnoreCase(request.getRequestURI(), ignores)) {
//                        if (!values.startsWith(CommonConstants.BASIE)) {
//                            requestTemplate.header(name, values);
//                        }
//                    }
//                    // 设置请求头白名单，防止上一段请求中的标志位影响下一段请求状态
                    switch (name) {
                        case GlobalConstants.LOCALE:
                        case GlobalConstants.USER:
                        case SecurityConstants.HEADER_OAUTH_USER_NAME:
                        case SecurityConstants.HEADER_OAUTH_USER_ID:
//                        case CommonConstants.TERMINAL:
//                        case CommonConstants.DEV_IP:
//                        case CommonConstants.DEV_SMART_CONNECT:
                        case GlobalConstants.TRACE_ID:
                            requestTemplate.header(name, values);
                            break;
                    }
                }
            }
            // 从 feign 中截取到当前服务的 serviceId，传递给 LemesFeignGrayReleaseRule
            request.setAttribute("serviceId", requestTemplate.feignTarget().name().replaceAll("/[\\s\\S]*", ""));
        }
    }

    @Bean
    @ConditionalOnMissingBean
    public HttpMessageConverters messageConverters(ObjectProvider<HttpMessageConverter<?>> converters) {
        return new HttpMessageConverters(converters.orderedStream().collect(Collectors.toList()));
    }

}
